/*
This file is part of "hp-like 4x4 keypad library".

"hp-like 4x4 keypad library" is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

"hp-like 4x4 keypad library" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with "hp-like 4x4 keypad library".  If not, see <http://www.gnu.org/licenses/>.

Copyleft: kenzanin[at]gmail[dot]com ||suka[dot]isnaini[at]politeknikaceh[dot]ac[dot]id
forum: solderen.tk/forum
*/

#include <avr/io.h>
#include <util/delay.h>
#include <avr/eeprom.h>
#include <string.h>
#include "password.h"
#include "keypad.h"
#include "lcd.h"

char  password[MAX_PASS]= {0};

void password_init(void)
{
    keypad_init();
}

void password_masuk(void)
{
    union
    {
        struct
        {
            int8_t lo:8;
            int8_t hi:8;
        };
        int16_t all;
    } status;

    union
    {
        struct
        {
            _Bool new_key:1;
            _Bool max_pass:1;
            _Bool key:1;
            _Bool backspace:1;
            _Bool backscape_again:1;
        };
        uint8_t all;
    } flag01;

    int8_t  password_ke=0;
    int8_t  var01=0;
    int8_t  var02=0;

    status.all=0;
    flag01.all=0;

    memset(password,'*',MAX_PASS);
    while(1)
    {
        flag01.new_key=0;
        flag01.backspace=0;
        flag01.key=0;

        if(password_ke>MAX_PASS)password_ke=MAX_PASS;
        //! todo:
        //! check apakah ada masalah pada compiler atau micro nya
        //! sehingga tidak bisa menghasilkan nilai variable negatif
        //! done -> memberikan nilai pada out of bound array menghasillkan
        //! nilai acak HINDARI
        if(password_ke<0 )password_ke=0;

        for(var01=0; var01<KEYPAD_JEDA; var01++)
        {
            lcd_gotoxy(POSISI_PASSWORD_X,POSISI_PASSWORD_Y);
            for(var02=0; var02<MAX_PASS; var02++)
            {
                lcd_putc(password[var02]);
            }

            status.all=hp_punya_hehe(flag01.key);
            if(status.hi==0x01)
            {
                _delay_ms(150);
            }
            else
            {
                if(status.lo == KEYPAD_ENTER ) return;
                if(status.lo == KEYPAD_BACKSPACE)
                {
                    flag01.backspace=1;
                    _delay_ms(200);
                    break;
                }
                if( password_ke<MAX_PASS )
                {
                    if(flag01.key && status.hi!=0x02)
                    {
                        flag01.new_key=1;
                        break;
                    }
                    else
                    {
                        password[password_ke]=status.lo;
                        var01=0;
                        flag01.key=1;
                        _delay_ms(200);
                    }
                }
            }
        }

        if(flag01.backspace)
        {
            if( password_ke > 0 && flag01.backscape_again )password_ke--;
            password[password_ke]='*';
            flag01.backscape_again=1;
        }
        else if((flag01.new_key || flag01.key) && password_ke < MAX_PASS && !flag01.backspace )
        {
            password_ke++;
            flag01.backscape_again=0;
        }
    }
}

void password_save(void)
{
    eeprom_write_block(password,ALAMAT_PASSWORD_EEPROM,MAX_PASS);
}

int8_t password_check(void)
{
    char _password[MAX_PASS]= {0};
    int8_t var02;

    eeprom_read_block(_password,ALAMAT_PASSWORD_EEPROM,MAX_PASS);
    for(var02=0; var02<MAX_PASS; var02++)
    {
        if(_password[var02]!=password[var02])return 1;
    }
    return 0;
}
